using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.ComponentModel;
using EcsSiteDSTableAdapters;
using System.Text;
using System.Collections.Generic;

/// <summary>
/// Summary description for EmpTilteHistoryBLL
/// </summary>
/// 

namespace SiteECSWeb
{
    [DataObject]
    public class EmpTilteHistoryBLL
    {
        private EmpTilteHistoryTableAdapter _EmpTilteHistoryAdapter = null;
        protected EmpTilteHistoryTableAdapter EmpTilteHistoryAdapter
        {

            get
            {
                if (_EmpTilteHistoryAdapter == null)
                {
                    _EmpTilteHistoryAdapter = new EmpTilteHistoryTableAdapter();
                }
                return _EmpTilteHistoryAdapter;

            }

        }
        #region Select EmpTilteHistoryData

        [DataObjectMethodAttribute(DataObjectMethodType.Select, true)]
        public EcsSiteDS.EmpTilteHistoryDataTable GetEmpTilteHistoryCurrentEmp()
        {
            return EmpTilteHistoryAdapter.GetEmpTitleHistoryCurrentEmpData();
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Select, true)]
        public EcsSiteDS.EmpTilteHistoryDataTable GetEmpTilteHistory()
        {
            return EmpTilteHistoryAdapter.GetEmpTilteHistoryData();
        }
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.EmpTilteHistoryDataTable GetEmpTilteHistoryByEmpTilteHistID(int EmpTilteHist_id)
        {
            return EmpTilteHistoryAdapter.GetEmpTilteHistoryDataByEmpTilteHistID(EmpTilteHist_id);
        }
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.EmpTilteHistoryDataTable GetEmpTilteHistoryByEmpID(int Ecsemp_id)
        {
            return EmpTilteHistoryAdapter.GetEmpTilteHistoryDataByEcsempId(Ecsemp_id);
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.EmpTilteHistoryDataTable GetEmpTilteHistoryByRowRank(int RowRank)
        {
            return EmpTilteHistoryAdapter.GetEmpTilteHistoryDataByRowRank(RowRank);
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public List<EmpTitleHisInfo> 
            GetEmpTilteHistoryCurrentEmpAdvanced( int EmpTitleId,string EmpNameToFind)
        {

            List<EmpTitleHisInfo> EmpTitleHisList = new List<EmpTitleHisInfo>();

            StringBuilder sbCode = new StringBuilder();
            string expression = string.Empty;

            EcsSiteDS.EmpTilteHistoryDataTable EmpTitleHisTable = GetEmpTilteHistoryCurrentEmp();
                            

            if (!string.IsNullOrEmpty(EmpNameToFind)
                || EmpTitleId > 0 )
            {
                if (!string.IsNullOrEmpty(EmpNameToFind))
                {
                    sbCode.Append("(Ecsemp_nmar LIKE'" + EmpNameToFind + "%') AND ");
                }
               
                if (EmpTitleId > 0)
                {
                    sbCode.Append("(EmpTitle_id =" + EmpTitleId + ") AND ");
                }
                if (sbCode.ToString().EndsWith(" AND "))
                {
                    expression = sbCode.ToString().Substring(0, sbCode.Length - 5);
                }
                DataView dv = new DataView((DataTable)EmpTitleHisTable);

                dv.RowFilter = expression;
                foreach (DataRowView EmpTitleHisRow in dv)
                {
                    EmpTitleHisInfo _empTitleHisInfo =
                    new EmpTitleHisInfo((int)EmpTitleHisRow["EmpTilteHist_id"],
                        (long)EmpTitleHisRow["RowRank"],
                        (string)EmpTitleHisRow["Ecsemp_nmar"], (int)EmpTitleHisRow["Ecsemp_id"],
                        (string)EmpTitleHisRow["EmpTitle_ar"], (int)EmpTitleHisRow["EmpTitle_id"],
                        (DateTime)EmpTitleHisRow["EmpTilteHist_AcquireDate"]);
                    EmpTitleHisList.Add(_empTitleHisInfo);
                }
            }
            else
            {
                foreach (EcsSiteDS.EmpTilteHistoryRow EmpTitleHisRow in EmpTitleHisTable)
                {
                    EmpTitleHisInfo _empTitleHisInfo =
                     new EmpTitleHisInfo(EmpTitleHisRow.EmpTilteHist_id,
                     EmpTitleHisRow.RowRank, EmpTitleHisRow.Ecsemp_nmar, EmpTitleHisRow.Ecsemp_id,
                     EmpTitleHisRow.EmpTitle_ar, EmpTitleHisRow.EmpTitle_id,
                         EmpTitleHisRow.EmpTilteHist_AcquireDate);
                    EmpTitleHisList.Add(_empTitleHisInfo);
                }
            }

            return EmpTitleHisList;
        }
        #endregion


        #region InsertFairs
        [DataObjectMethodAttribute(DataObjectMethodType.Insert, true)]
        public bool AddEmpTilteHistory(DateTime? EmpTilteHist_AcquireDate, DateTime? EmpTilteHist_EDate,
            int? EmpTitle_id, int Ecsemp_id, string EmpTilteHist_Note, int? City_id, int? DepCat_id,
            int? EmpPostition_Id)
        {

            EcsSiteDS.EmpTilteHistoryDataTable EmpTilteHistoryTable = new EcsSiteDS.EmpTilteHistoryDataTable();
            EcsSiteDS.EmpTilteHistoryRow EmpTilteHistoryRow = EmpTilteHistoryTable.NewEmpTilteHistoryRow();

            if (EmpTilteHist_AcquireDate == null) EmpTilteHistoryRow.SetEmpTilteHist_AcquireDateNull();
            else EmpTilteHistoryRow.EmpTilteHist_AcquireDate = EmpTilteHist_AcquireDate.Value;
            if (EmpTilteHist_EDate == null) EmpTilteHistoryRow.SetEmpTilteHist_EDateNull(); else EmpTilteHistoryRow.EmpTilteHist_EDate = EmpTilteHist_EDate.Value;
            if (EmpTitle_id == null) EmpTilteHistoryRow.SetEmpTitle_idNull(); else EmpTilteHistoryRow.EmpTitle_id = EmpTitle_id.Value;
            if (string.IsNullOrEmpty(EmpTilteHist_Note)) EmpTilteHistoryRow.SetEmpTilteHist_NoteNull(); else EmpTilteHistoryRow.EmpTilteHist_Note = EmpTilteHist_Note;
            if (City_id == null) EmpTilteHistoryRow.SetCity_idNull(); else EmpTilteHistoryRow.City_id = City_id.Value;
            if (DepCat_id == null) EmpTilteHistoryRow.SetDepCat_idNull(); else EmpTilteHistoryRow.DepCat_id = DepCat_id.Value;
            if (EmpPostition_Id == null) EmpTilteHistoryRow.SetEmpPostition_IdNull(); else EmpTilteHistoryRow.EmpPostition_Id = EmpPostition_Id.Value;

            EmpTilteHistoryRow.Ecsemp_id = Ecsemp_id;

            EmpTilteHistoryTable.AddEmpTilteHistoryRow(EmpTilteHistoryRow);

            int rowsAffected = EmpTilteHistoryAdapter.Update(EmpTilteHistoryTable);

            return rowsAffected == 1;
        }

        #endregion

        #region UpdateFairs
        [DataObjectMethodAttribute(DataObjectMethodType.Update, true)]
        public bool UpdatEmpTilteHistory(DateTime? EmpTilteHist_AcquireDate, DateTime? EmpTilteHist_EDate,
            int? EmpTitle_id, int Ecsemp_id, string EmpTilteHist_Note, int? City_id, int? DepCat_id,
            int? EmpPostition_Id, int EmpTilteHist_id)
        {
            EcsSiteDS.EmpTilteHistoryDataTable EmpTilteHistoryTable =
                EmpTilteHistoryAdapter.GetEmpTilteHistoryDataByEmpTilteHistID(EmpTilteHist_id);
           
            if (EmpTilteHistoryTable.Count == 0)
            {
                return false;
            }

            EcsSiteDS.EmpTilteHistoryRow EmpTilteHistoryRow = EmpTilteHistoryTable[0];
         
         
            if (EmpTilteHist_AcquireDate == null) EmpTilteHistoryRow.SetEmpTilteHist_AcquireDateNull();
            else EmpTilteHistoryRow.EmpTilteHist_AcquireDate = EmpTilteHist_AcquireDate.Value;
            if (EmpTilteHist_EDate == null) EmpTilteHistoryRow.SetEmpTilteHist_EDateNull(); else EmpTilteHistoryRow.EmpTilteHist_EDate = EmpTilteHist_EDate.Value;
            if (EmpTitle_id == null) EmpTilteHistoryRow.SetEmpTitle_idNull(); else EmpTilteHistoryRow.EmpTitle_id = EmpTitle_id.Value;
            if (string.IsNullOrEmpty(EmpTilteHist_Note)) EmpTilteHistoryRow.SetEmpTilteHist_NoteNull(); else EmpTilteHistoryRow.EmpTilteHist_Note = EmpTilteHist_Note;
            if (City_id == null) EmpTilteHistoryRow.SetCity_idNull(); else EmpTilteHistoryRow.City_id = City_id.Value;
            if (DepCat_id == null) EmpTilteHistoryRow.SetDepCat_idNull(); else EmpTilteHistoryRow.DepCat_id = DepCat_id.Value;
            if (EmpPostition_Id == null) EmpTilteHistoryRow.SetEmpPostition_IdNull(); else EmpTilteHistoryRow.EmpPostition_Id = EmpPostition_Id.Value;

            EmpTilteHistoryRow.Ecsemp_id = Ecsemp_id;

            int rowsAffected = EmpTilteHistoryAdapter.Update(EmpTilteHistoryRow);

            return rowsAffected == 1;

        }

        #endregion

        #region DeleteFairs

        [DataObjectMethodAttribute(DataObjectMethodType.Delete, true)]
        public bool DeleteEmpTilteHistory(int EmpTilteHist_id)
        {
            int rowsAffected = EmpTilteHistoryAdapter.Delete(EmpTilteHist_id);
            return rowsAffected == 1;
        }
        #endregion


    }
}

